package cn.zhy.user.controller;

import cn.zhy.basic.exception.BusinessException;
import cn.zhy.basic.util.AjaxResult;
import cn.zhy.user.dto.LoginDto;
import cn.zhy.user.service.ILogininfoService;
import org.apache.xmlbeans.impl.xb.ltgfmt.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService iLogininfoService;

    //账号密码登录
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = iLogininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);

        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");
        }


    }


    //微信登录接口
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String, String> map) {
        //使用map和dto都可以
        try {
            String code = map.get("code");
            return iLogininfoService.wechat(code);

        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");

        }
    }


    //微信登录接口
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String, String> map) {
        //使用map和dto都可以
        try {
            return iLogininfoService.wechatBinder(map);

        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");

        }

    }








}